package com.itsm.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itsm.entity.Approve;
import com.itsm.entity.Process;
import com.itsm.entity.User;
import com.itsm.service.mapper.ProcessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

//@DS("master")
@Service
public class ProcessUserService {
    @Autowired
    private ProcessMapper processMapper;

    @Autowired
    private SendService sendService;

    @Autowired
    private UserService userService;
    @Autowired
    private ApproveService approveService;

    public Process getUserProcess(String processNo){

        return processMapper.selectById(processNo);
    }
    /**
     * 创建人工单查询（完成和未完成）
     * @param name 根据名字信息
     * @param state 判断是否完成
     * @return 工单信息
     */
    public List<Process> selectUserProcess(String name, Integer state){
        QueryWrapper<Process> wrapper = new QueryWrapper();
        Map<String, Object> queryParamsMap = new HashMap<>();
        queryParamsMap.put("is_delete", 1);
        queryParamsMap.put("create_name", name);
        if(state!=null){
            queryParamsMap.put("state", state);
        }
        wrapper.allEq(queryParamsMap).orderByDesc("process_no");
        return processMapper.selectList(wrapper);
    }


    public List<Process> selectUserOver(String name, Integer state){
        QueryWrapper<Process> wrapper = new QueryWrapper();
        Map<String, Object> queryParamsMap = new HashMap<>();
        queryParamsMap.put("create_name", name);
        queryParamsMap.put("is_delete", 1);
        queryParamsMap.put("state", state);
        wrapper.allEq(queryParamsMap).orderByDesc("process_no");
        wrapper.lambda().isNull(Process::getScore);
        return processMapper.selectList(wrapper);
    }

    public List<Process> selectUserOver1(String name, Integer state){
        QueryWrapper<Process> wrapper = new QueryWrapper();
        Map<String, Object> queryParamsMap = new HashMap<>();
        queryParamsMap.put("is_delete", 1);
        queryParamsMap.put("create_name", name);
        queryParamsMap.put("state", state);
        wrapper.allEq(queryParamsMap).orderByDesc("process_no");
        wrapper.lambda().isNotNull(Process::getScore);
        return processMapper.selectList(wrapper);
    }
    /**
     * 查询最后一条数据，获取单号，用于流水号生成
     * @return 流水号
     */
    public String selectEndNo(){
        Process process = processMapper.selectOne(new QueryWrapper<Process>().orderByDesc("process_no").last("limit 1"));
        if(process==null){
            return null;
        }
        return process.getProcessNo();
    }

    /**
     * 工单创建
     * @param process 插入的数据
     * @return
     */
    public Integer addProcess(Process process,String approve1,String approve2) throws IOException {

        //流水号自动生成
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String nowDate = sdf.format(new Date());
        //个人信息
        User user = userService.selectUser(process.getUserid());
        String processNo1;
        String processNo = this.selectEndNo();
        if (processNo==null) {
            processNo1 = nowDate+"001";
        }else{
            String oldDate = processNo.substring(0, 8);
            if (!nowDate.equals(oldDate)){
                processNo1 = nowDate+"001";
            }
            else {
                processNo1 = String.valueOf(Long.valueOf(processNo)+1);
            }
        }

        process.setCtime(new Date());
        process.setState(0);
        process.setIsAccepted(0);
        process.setProcessNo(processNo1);
        process.setPlant(user.getPlant());
        int col = processMapper.insert(process);
        //判断是否需要审批，需要就创建审批流
        if(approve1.length() != 0||approve2.length() != 0){
            Approve approve = new Approve();
            approve.setProcessNo(processNo1);
            approve.setStep(1);
            approve.setStatus(0);
            approve.setApprover(approve1);
            approve.setSubTime(new Date());
            approve.setName(process.getCreateName());
            approve.setCount(1);
            approve.setStep1(1);
            //推送企微消息给单位审批人
            sendService.sendSup(process,approve1);
            //创建第一个审批信息
            approveService.addApprove(approve);
            if (!approve1.equals(approve2)) {
                //创建第二个审批信息
                approve.setApprover(approve2);
                approve.setId(null);
                approve.setSubTime(null);
                approve.setStep1(2);
                approveService.addApprove(approve);
            }
            //推送企微给上级
//            sendService.sendSup(process);
        }else {
            sendService.sendDesk(process);
        }

        return col;
    }



}
